package ucd.csi.comp30290.fall2011.ebbt;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import net.sourceforge.cobertura.coveragedata.TouchCollector;
import net.sourceforge.cobertura.instrument.InstrumentedClass;
import net.sourceforge.cobertura.instrument.PublicClassInstrumenter;

public class Executor {
    
    private static Random random = new Random();
    
    public static void main(String[] argv) throws Exception {
        List<Coverage> list = new ArrayList<Coverage>();
        for (int i = 0; i< 10; i++) {
            list.add(test(new NavigationBlackBox(), generate()));
            //list.add(test(new SimpleBlackBox(), generate()));
        }
        //list.add(test(new SimpleBlackBox(), generate2()));
        //list.add(test(new SimpleBlackBox(), generate3()));
        for (Coverage coverage : list) {
            System.err.println(coverage);
        }
    }
    
    private static Coverage test(BlackBox impl, double[] vals) throws Exception {
        final String clazzName = impl.getClass().getName();
        InstrumentedClass ic = new PublicClassInstrumenter().instrument(impl.getClass());
        BlackBox box = (BlackBox) new HackedClassLoader().loadClass(clazzName, ic.getBytes()).newInstance();
        box.calculate(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
        TouchCollector.applyTouchesOnProjectData(ic.getProject());
        return new Coverage(
            ic.getData().getNumberOfValidLines(),
            ic.getData().getNumberOfValidBranches(),
            ic.getData().getLineCoverageRate(),
            ic.getData().getBranchCoverageRate());
    }
    
    private static double[] generate2() {
        double[] vals = generate();
        vals[0] = 1024.0;
        return vals;
    }
    
    private static double[] generate3() {
        double[] vals = generate();
        vals[0] = 0.0;
        return vals;
    }
    
    private static double[] generate() {
        return new double[] { 
                random.nextDouble() * 360,
                random.nextDouble() * 360,
                random.nextDouble() * 360,
                random.nextDouble() * 360,
                random.nextDouble() * 360,
                random.nextDouble() * 360
        };
    }
    
}
